/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.university.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.Dict;
import com.thinkgem.jeesite.modules.sys.entity.Role;
import com.thinkgem.jeesite.modules.sys.service.DictService;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.university.entity.University;
import com.thinkgem.jeesite.modules.university.service.UniversityService;

/**
 * 学校Controller
 * @author 朱俊吉
 * @version 2017-12-12
 */
@Controller
@RequestMapping(value = "${adminPath}/university/university")
public class UniversityController extends BaseController {

	@Autowired
	private UniversityService universityService;
	@Autowired
	private DictService dictService;
	
	@ModelAttribute
	public University get(@RequestParam(required=false) String id) {
		University entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = universityService.get(id);
		}
		if (entity == null){
			entity = new University();
		}
		return entity;
	}
	
	@RequiresPermissions("university:university:view")
	@RequestMapping(value = {"list", ""})
	public String list(University university, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<University> page = universityService.findPage(new Page<University>(request, response), university); 
		List<Dict> allLevels = Lists.newArrayList();
		Dict dict = new Dict();
		dict.setType("university_level");
		allLevels = dictService.findList(dict);
		model.addAttribute("allLevels", allLevels);
		model.addAttribute("page", page);
		return "modules/university/universityList";
	}

	@RequiresPermissions("university:university:view")
	@RequestMapping(value = "form")
	public String form(University university, Model model) {
		
		if(StringUtils.isNotBlank(university.getId())){
			//查询学校办学层次
			university.setLevelList(universityService.getLevelList(university));
		}
		List<Dict> allLevels = Lists.newArrayList();
		Dict dict = new Dict();
		dict.setType("university_level");
		allLevels = dictService.findList(dict);
		model.addAttribute("allLevels", allLevels);
		model.addAttribute("university", university);
		return "modules/university/universityForm";
	}

	@RequiresPermissions("university:university:edit")
	@RequestMapping(value = "save")
	public String save(University university, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, university)){
			return form(university, model);
		}
		// 办学层次有效性验证，过滤没选中的办学层次
		List<Dict> levelList = Lists.newArrayList();
		List<String> levelValList = university.getLevelValList();
		Dict dict = new Dict();
		dict.setType("university_level");
		for (Dict l : dictService.findList(dict)){
			if (levelValList.contains(l.getValue())){
				levelList.add(l);
			}
		}
		university.setLevelList(levelList);
		// 保存大学信息
		universityService.save(university);
		addMessage(redirectAttributes, "保存学校成功");
		return "redirect:"+Global.getAdminPath()+"/university/university/?repage";
	}
	
	@RequiresPermissions("university:university:edit")
	@RequestMapping(value = "delete")
	public String delete(University university, RedirectAttributes redirectAttributes) {
		universityService.delete(university);
		addMessage(redirectAttributes, "删除学校成功");
		return "redirect:"+Global.getAdminPath()+"/university/university/?repage";
	}

}